package classes.view.component
{
	import classes.controller.EDataObject;
	import classes.model.VOData;
	
	import com.carlcalderon.arthropod.Debug;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quad;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Popup extends Sprite
	{
		private var _bg_mc:MovieClip;
		private var _friendAdd_mc:PopupFriends;
		private var _setting_mc:PopupSetting;
		private var _room_mc:PopupMakeRoom;
		private var _grade_mc:PopupGrade;
		private var _score_mc:PopupScore;
		private var _scoreSingle_mc:PopupFinish;
		private var _personal_mc:PopupPersonal;
		private var _readyScreen:ReadyScreen;
		private var _alt_mc:PopupAlert;
		private var _singlePause:PopupSinglePause;
		private var _currentState:String;
		private var _active_mc:*;
		
		
		public function Popup()
		{
			super();
			
			setLayOut();
			defaultSetting();
		}
		
		private function setLayOut():void
		{
			this._bg_mc	=	this.getChildByName('bg_mc') as MovieClip;
		}
		private function defaultSetting():void
		{
			this.visible = false;
			this._bg_mc.alpha = 0;
		}
		public function util_popup_select($select:String, $data:Object):void
		{
			//bg 띄우기.
			this.visible 		= true;
			this.alpha			= 1;
			this._currentState  = $select;
			this._bg_mc.alpha 	= 0;
			
			TweenLite.to(_bg_mc, 0.3, {autoAlpha:0.7, ease:Quad.easeOut});
			
			
			switch($select){
				case 'info':
					//성적분석 
					_grade_mc  			= new PopupGrade();
					_active_mc 			= _grade_mc;
					this._active_mc.y	= 8;
					
					addChild(_active_mc);
					TweenMax.to(_active_mc, .6, { y:0, autoAlpha:1, onComplete:_active_mc.data_init(), ease:Quart.easeOut } );
					break;
				
				case 'friend':
					//친구관리  
					_friendAdd_mc 		= new PopupFriends();
					_active_mc 			= _friendAdd_mc;
					this._active_mc.y	= 8;
					
					addChild(_active_mc);
					TweenMax.to(_active_mc, .6, { y:0,  autoAlpha:1, onComplete:_friendAdd_mc.data_init(), ease:Cubic.easeOut } );
					break;
				
				case 'setting':
					//설정 
					_setting_mc			= new PopupSetting();
					_active_mc			= _setting_mc;
					this._setting_mc.y	= 8;
					addChild(_setting_mc);
					TweenMax.to(_setting_mc, .6, { y:0, autoAlpha:1, onComplete:_setting_mc.dataInit(), ease:Quart.easeOut } );
					break;
				
				case 'createroom' :
					//방만들기 
					trace('room make');
					_room_mc = new PopupMakeRoom();
					_active_mc = _room_mc;
					addChild(_active_mc);
					
					this._active_mc.y	= 8;
					
					TweenMax.to(_active_mc, .3, { y:0, autoAlpha:1, onComplete:_active_mc.modeSelect(1), ease:Cubic.easeOut } );
					break;
				
				case 'subject' :
					//방설정 (디자인 03, 04, 05 파일.)
					trace('subject_popup on');
					_room_mc = new PopupMakeRoom();
					//_room_mc.settingMode();
					_active_mc = _room_mc;
					addChild(_active_mc);
					
					this._active_mc.y	= 8;
					
					TweenMax.to(_active_mc, .3, {y:0, autoAlpha:1, onComplete:_active_mc.modeSelect(4), ease:Cubic.easeOut } );
					break;
				
				case 'gameSetting':
					//게임 대기방에서의 설정버튼을 눌렀을때 나오는 팝업 (
					//trace('game_setting_popup on');
					_room_mc = new PopupMakeRoom();
					_active_mc = _room_mc;
					addChild(_active_mc);
					
					this._active_mc.y	= 8;
					
					TweenMax.to(_active_mc, .3, {y:0, autoAlpha:1, onComplete:_active_mc.modeSelect(3), ease:Cubic.easeOut } );
					break;
				
				case 'gameEnd':
					//게임 끝났을 경우
					//trace('____'+$data);
					
					_score_mc = new PopupScore();
					_active_mc = _score_mc;
					addChild(_score_mc);
					
					this._active_mc.y	= 8;
					
					TweenMax.to(_active_mc, .3, {y:0,  autoAlpha:1, onComplete:_score_mc.data_init(String($data)), ease:Cubic.easeOut } );
					break;
				
				case 'single':
					//싱글룸 방만들기
					_room_mc = new PopupMakeRoom();
					_active_mc = _room_mc;
					//	_room_mc.singlemode_chk = true;
					addChild(_active_mc);
					
					
					this._active_mc.y	= 8;
					TweenMax.to(_active_mc, .3, {y:0, autoAlpha:1, onComplete:_active_mc.modeSelect(2), ease:Cubic.easeOut } );
					break;
				
				case 'singlegameEnd':
					//싱글룸 게임 끝났을 경우
					_scoreSingle_mc = new PopupFinish();
					_active_mc = _scoreSingle_mc;
					addChild(_scoreSingle_mc);
					
					this._active_mc.y	= 8;
					TweenMax.to(_active_mc, .3, {y:0, autoAlpha:1, onComplete:_scoreSingle_mc.data_init(String($data)), ease:Cubic.easeOut } );
					break;
				
				case 'alertSingle_1':
					VOData.getDataInstance().correct = true;
					
					_alt_mc = new PopupAlert();
					
					_active_mc = _alt_mc;
					addChild(_active_mc);
					_bg_mc.alpha = 0.7;
					_active_mc.on("오답노트에서 선택된 문장으로 훈련합니다.<br>계하시겠습니까?", "시작하기", "닫기", 0);
					
					break;
				
				case 'alertSingle_2':
					
					_alt_mc = new PopupAlert();
					_active_mc = _alt_mc;
					addChild(_active_mc);
					_active_mc.alpha = 0;
					_active_mc.on("오답노트에서 선택된 문장이 있습니다.<br>과목/범위를 다시 설정하시겠습니까?", "설정하기", "돌아가기", 1);
					TweenMax.to(_active_mc, .2, { autoAlpha:1, ease:Cubic.easeOut } );
					
					break;
				
				case 'singlePause' :
					_singlePause = new PopupSinglePause();
					_active_mc = _singlePause;
					addChild(_singlePause);
					TweenMax.to(_active_mc, .2, { autoAlpha:1, ease:Cubic.easeOut } );
					break;
				
				case 'gamePersonal':
					//개인정보화면
					_personal_mc = new PopupPersonal();
					_active_mc = _personal_mc;
					addChild(_personal_mc);
					TweenMax.to(_active_mc, .3, { autoAlpha:1, onComplete:_personal_mc.data_init($data), ease:Cubic.easeOut } );
					break;
				
				case 'readyScreenOn':
					//레디스크린 (완료)
					_readyScreen 					= new ReadyScreen();
					_active_mc 						= _readyScreen;
					
					this._readyScreen.addEventListener("ReadyOff",	offReadyScreen);
					addChild(_active_mc);
					TweenMax.to(_active_mc, 0, { autoAlpha:1, onComplete:_readyScreen.readyscreen_switch(true), ease:Cubic.easeOut } );
					
					break;
				
				case 'readyScreenOff':
					//레디스크린 (완료)
					this._readyScreen.removeEventListener("ReadyOff",	offReadyScreen);
					_readyScreen.readyscreen_switch(false);
					popupCloseAni();
					break;
				
			}
			_active_mc.addEventListener(EDataObject.EVENT_DATAOBJECT, childEvent);
		}
		
		private function childEvent(e:EDataObject):void 
		{
			popupClose(e._object[0]);
		}
		
		private function popupClose($str:String):void{
			
			switch($str) {
				case "close":
					popupCloseAni();
					break;
				
				case "create":
					//방 만들기.
					trace('create room');
					popupCloseAni();
					this.dispatchEvent(new EDataObject(["createroom"], 	EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "singleCreate":
					//싱글방 만들기.
					trace("single room");
					this.visible = false;
					this.alpha   = 0;
					removeChild(_active_mc);
					
					dispatchEvent(new EDataObject(["singleCreate"], 	EDataObject.EVENT_DATAOBJECT));
					break;
				case "list" :
					popupCloseAni();
					dispatchEvent(new EDataObject(["list"], 			EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "waiting" :
					popupCloseAni();
					dispatchEvent(new EDataObject(["waiting"], 			EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "singleResume":
					popupCloseAni();
					dispatchEvent(new EDataObject(["singleResume"], 	EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "singleList" :
					popupCloseAni();
					dispatchEvent(new EDataObject(["singleList"], 		EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "singleScore" :
					
					popupCloseAni();
					dispatchEvent(new EDataObject(["singleScore"], 		EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "withGame":
					popupCloseAni();
					dispatchEvent(new EDataObject(["withGame"], 		EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "startCorrectSingleMode":
					//popupCloseAni();
					removeChild(_active_mc);
					dispatchEvent(new EDataObject(["startCorrectSingleMode"], 		EDataObject.EVENT_DATAOBJECT));
					break;
				
				case "onMakeSinglePopup" :
					removeChild(_active_mc);
					util_popup_select("single", "");
					break;
				
			}
			
		}
		private function popupCloseAni():void{TweenMax.to(this, .2, { autoAlpha:0, onComplete:popupDelete, ease:Cubic.easeOut } );}
		
		
		private function popupDelete():void{
			
			if(this.contains(_active_mc))removeChild(_active_mc);
		}
		private function offReadyScreen(e:Event):void{
			
			dispatchEvent(e);
			
			
		}
		
	}
}
